<template>
	<div class="resource_hand">
		<el-radio-group class="type" v-model="checkedType">
			<el-radio class="el-icon-sw-open-folder" :style="{ '--iconColor': '#ce77d9' }" label="0">全部</el-radio>
			<el-radio v-for="(item, index) in resourceTypes" :key="index" :class="item.icon" :style="{ '--iconColor': item.color }" :label="item.value">{{ item.msg }}</el-radio>
		</el-radio-group>
		<ul class="sort">
			<li :class="[{ active: checkedSort === '1' }, sortOrder]" @click="handleCheckedSort($event, '1')">发布时间</li>
			<li :class="[{ active: checkedSort === '2' }, sortOrder]" @click="handleCheckedSort($event, '2')">观看次数</li>
			<li :class="[{ active: checkedSort === '3' }, sortOrder]" @click="handleCheckedSort($event, '3')">下载次数</li>
		</ul>
	</div>
</template>

<script>
import { mapActions } from 'vuex';
export default {
	props: ['data'],
	data() {
		return {
			checkedType: '0',
			checkedSort: '1',
			sortOrder: 'desc',
			resourceTypes: []
		};
	},
	watch: {
		checkedType(val) {
			this.$emit('switch', val);
		}
	},
	mounted() {
		this.getEnumDataByCode('FILE_TYPE').then(types => {
			this.resourceTypes = types.map(item => {
				let obj = { ...item };
				switch (obj.value) {
					case '1':
						obj['icon'] = 'el-icon-sw-image';
						obj['color'] = '#6ccec7';
						break;
					case '2':
						obj['icon'] = 'el-icon-sw-audio';
						obj['color'] = '#60c1e9';
						break;
					case '3':
						obj['icon'] = 'el-icon-sw-video';
						obj['color'] = '#ee518c';
						break;
					case '4':
						obj['icon'] = 'el-icon-sw-document';
						obj['color'] = '#ec7b7b';
						break;
				}
				return obj;
			});
		});
	},
	methods: {
		...mapActions(['getEnumDataByCode']),

		//选择排序方式
		handleCheckedSort(e, val) {
			let orderNum = 6;
			let domClassArr = e.target.classList;
			if (this.checkedSort === val) {
				if (!domClassArr.contains('desc')) {
					domClassArr.remove('asc');
					domClassArr.add('desc');
				} else {
					domClassArr.remove('desc');
					domClassArr.add('asc');
				}
			} else {
				this.checkedSort = val;
				domClassArr.add('desc');
			}
			if (this.checkedSort === '1') {
				orderNum = !domClassArr.contains('desc') ? 5 : 6;
			} else if (this.checkedSort === '2') {
				orderNum = !domClassArr.contains('desc') ? 3 : 1;
			} else if (this.checkedSort === '3') {
				orderNum = !domClassArr.contains('desc') ? 4 : 2;
			}
			this.$emit('sort', orderNum);
		}
	}
};
</script>

<style lang="scss" scoped>
.resource_hand {
	display: flex;
	align-items: center;
	justify-content: space-between;
	width: 100%;
	height: 44px;
	margin-top: 40px;
	.type ::v-deep {
		display: flex;
		height: 44px;
		.el-radio {
			display: flex;
			align-items: center;
			padding: 0 24px;
			margin-right: 24px;
			border-radius: 22px;
			background-color: #fff;
			&::before {
				font-size: 24px;
				color: var(--iconColor);
			}
			&.is-checked {
				background-color: var(--primary-color-active);
				&::before {
					color: #fff;
				}
				.el-radio__label {
					color: #fff;
				}
			}
		}
		.el-radio__input {
			display: none;
		}
		.el-radio__label {
			font-size: 16px;
		}
	}
	.sort {
		display: flex;
		li {
			position: relative;
			font-size: 16px;
			margin-left: 30px;
			padding: 5px 15px 5px 0;
			line-height: 1;
			user-select: none;
			cursor: pointer;
			&:first-child {
				margin-left: 0;
			}
			&::before,
			&::after {
				position: absolute;
				right: 0;
				content: '';
				border-left: solid 4px transparent;
				border-right: solid 4px transparent;
			}
			&::before {
				top: 6px;
				border-bottom: solid 6px rgba(0, 0, 0, 0.4);
			}
			&::after {
				bottom: 6px;
				border-top: solid 6px rgba(0, 0, 0, 0.4);
			}
			&:hover {
				color: var(--primary-hover);
			}
			&.active {
				color: var(--primary-active);
				&.desc::after {
					border-top-color: var(--primary-active);
				}
				&.asc::before {
					border-bottom-color: var(--primary-active);
				}
			}
		}
	}
}
</style>
